#include "relaybutton.h"
#include "maindialog.h"

#include <QApplication>
#include <QtGui>

RelayButton::RelayButton(RelayData *relayData, QWidget *parent) :
    QPushButton(parent)
{
    m_pRelayData = relayData;

    m_pixmapBackground.load(tr("://images/btn_background.png"));

    setEnabled(m_pRelayData->notUsed ? false : true);
    updateStatus();
}

RelayButton::~RelayButton()
{
}

QSize RelayButton::sizeHint() const
{
    QSize size = QPushButton::sizeHint();

    if (m_pixmapBackground.isNull()) {
        size.setWidth(DefWidth);
        size.setHeight(DefHeight);
    } else {
        size = m_pixmapBackground.size();
    }

    return size;
}

void RelayButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter;

    painter.begin(this);
    if (false == m_pixmapBackground.isNull()) {
        painter.drawPixmap(0, 0, m_pixmapBackground);
        painter.drawText(QRectF(0, 0, m_pixmapBackground.size().width(), 34), Qt::AlignCenter, m_pRelayData->name);
        if (false == m_pixmapStatus.isNull()) {
            painter.drawPixmap(4, 34, m_pixmapStatus);
        }
    } else {
        painter.drawText(QRectF(0,0,DefWidth,DefHeight), Qt::AlignCenter, m_pRelayData->name);
    }
    painter.end();

}

void RelayButton::updateStatus()
{
    //QString text = QString("%1-%2").arg(m_pRelayData->name).arg(m_pRelayData->status);
    //setText(text);

    if (m_pRelayData->notUsed) {
        return;
    }

    QString fileName;
    switch (m_pRelayData->status) {
    case RELAY_STATUS_OFF:
        fileName = tr("://images/status_off.png");
        break;
    case RELAY_STATUS_ON:
        fileName = tr("://images/status_on.png");
        if (m_pRelayData->pushDelay) {
            int msec = m_pRelayData->pushDelay * 1000;
            QTimer::singleShot(msec, this, SLOT(onAutoPush()));
        }
        break;
    case RELAY_STATUS_PENDING:
        fileName = tr("://images/status_pending.png");
        break;
    case RELAY_STATUS_SHUTDOWN:
        fileName = tr("://images/status_shutdown.png");
        {
            int msec = ((MainDialog *)nativeParentWidget())->shutDownDelay() * 1000;
            QTimer::singleShot(msec, this, SLOT(onShutDown()));
        }
        break;
    case RELAY_STATUS_UNKNOWN:
        fileName = tr("://images/status_unknown.png");
        break;
    case RELAY_STATUS_NOTUSED:
    default:
        break;
    }
    if (fileName.length()) {
        m_pixmapStatus.load(fileName);
    }
    update();
}

void RelayButton::updateMinute(int dayOfWeek, int hour, int minute)
{
    for (int i=0; i<m_pRelayData->alarms.size(); i++) {
        if ((m_pRelayData->alarms[i].dayOfWeek & (1<<dayOfWeek))
                && (m_pRelayData->alarms[i].hour == hour)
                && (m_pRelayData->alarms[i].minute == minute)) {
            RelayEvent *event = new RelayEvent(m_pRelayData, m_pRelayData->alarms[i].action);
            QApplication::postEvent(nativeParentWidget(), event);
        }
    }
}

void RelayButton::onAutoPush()
{
    RelayEvent *event = new RelayEvent(m_pRelayData, 0);
    QApplication::postEvent(nativeParentWidget(), event);
}

void RelayButton::onShutDown()
{
    RelayEvent *event = new RelayEvent(m_pRelayData, 0);
    QApplication::postEvent(nativeParentWidget(), event);
}
